package ciyuan.Block.TypeInterface;

import ciyuan.Gui.Component.ItemButton;
import ciyuan.MainClass;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class CraftTableScreen<T extends MachineContainer> extends MachineScreen<T> {

    private static final ResourceLocation BACKGROUND_RESOURCE = new ResourceLocation(MainClass.MODID, "textures/gui/container/craft_table.png");
    private static final ResourceLocation CRAFT_PROGRESS = new ResourceLocation(MainClass.MODID, "textures/gui/container/craft_progress.png");
    private static final AtomicInteger count = new AtomicInteger(0);
    private static final AtomicInteger top = new AtomicInteger(0);
    private final Map<Item, List<ItemStack>> craftMap;
    private Item selectedItem = null;

    public CraftTableScreen(T cntainer, PlayerInventory playerInventory, ITextComponent titleText, Map<Item, List<ItemStack>> craftMap) {
        super(cntainer, playerInventory, titleText, BACKGROUND_RESOURCE, 176, 184);
        this.craftMap = craftMap;
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float particleTicks) {
        super.render(matrixStack, mouseX, mouseY, particleTicks);
        this.drawNeedMaterialList();
    }

    @Override
    protected void renderBg(MatrixStack matrixStack, float particleTicks, int mouseX, int mouseY) {
        super.renderBg(matrixStack, particleTicks, mouseX, mouseY);
        this.drawProgressBar(matrixStack);
    }

    @Override
    protected void renderLabels(MatrixStack matrixStack, int mouseX, int mouseY) {
        this.font.draw(matrixStack, this.title, 7, -2, 4210752);
        this.font.draw(matrixStack, this.inventory.getDisplayName(), 7, 81, 4210752);
    }

    @Override
    protected void init() {
        super.init();
        this.drawCanCraftItemList();
    }

    protected void drawCanCraftItemList() {
        int left = this.leftPos + 7;
        top.set(this.topPos + 8);
        count.set(0);
        craftMap.keySet().forEach(item -> {
            this.addButton(new ItemButton(left + count.get() * 18, top.get(), this.title, item, button -> {
                this.selectedItem = item;
            }, this.itemRenderer));
            count.addAndGet(1);
            if (count.get() >= 9) {
                count.set(0);
                top.addAndGet(18);
            }
        });
    }

    protected void drawNeedMaterialList() {
        if (this.selectedItem == null) {
            return;
        }
        int left = this.leftPos + 8;
        int top = this.topPos + 44;
        count.set(0);

        craftMap.get(this.selectedItem).forEach(itemStack -> {
            this.itemRenderer.renderAndDecorateFakeItem(itemStack, left + count.get() * 18, top);
            this.itemRenderer.renderGuiItemDecorations(this.font, itemStack, left + count.get() * 18, top);
            count.addAndGet(1);
        });
    }

    protected void drawProgressBar(MatrixStack matrixStack) {
        int left = this.leftPos + 43;
        int top = this.topPos + 82;
        int progress = (int) (this.getMenu().getMachineTileEntity().getProgress() / 800.0 * 126);

        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(CRAFT_PROGRESS);
        blit(matrixStack, left, top, 0, 0, progress, 8, 126, 8);
    }

}
